import {
  myRequesy
} from '../../request/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //用户动态数组
    Detailsobj: null,
    // 选项卡数组
    select: [{
        name: "按时间",
        status: false,
        id: 0
      },
      {
        name: "按热度",
        status: true,
        id: 1
      }
    ],
    commentlist: [], //评论列表
    Hizfs: false, //转发底部弹窗
    Hireveal: false, //底部弹窗事件
    publish: false, //发表评论显示隐藏底部弹框事件
    Switch: true, //控制底部发表或回复弹窗事件
    removeEnt: false, //控制底部删除回复/评论弹窗事件
    follreveal: false, //关注弹窗事件
    values: '', //发表评论的内容
    deleteId: '', //根据id删除对应的评论或回复信息
    detailsId: null, //详情id
    deleHiShow: false, //判断点击删除评论/或删除回复
    index: 1, //判断当前是否点赞评论
    HeatPage: 1, //热度评论页码值
    nextHeat: null, //判断热度是否有下一页数据
    active: 1, //判断评论热度或者最新
    absSipwerList: [], //保存广告列表
    stateList: [],
    deleteId: [],
    listId: [],
    shut: true, //控制新人专享
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      detailsId: options.scene ? options.scene : options.id,
    })
    console.log(options);
    
    this.getComment()
    this.getDynamicDetails()
    this.getadsList()

    // if (wx.getStorageSync('listId')) {
    //   this.setData({
    //     listId: wx.getStorageSync('listId')
    //   })
    // }

    // status用户传过来的/用户点击评论显示发表底部弹窗
    this.setData({
      publish: options.status
    })
  },

  btnCance() {
    this.setData({
      follreveal: false
    })
  },
  // 关注或取消关注
  headleAtte() {
    let thst = this
    let {
      Detailsobj
    } = thst.data
    if (Detailsobj.follow_state !== 0) {
      this.setData({
        follreveal: true
      })
    } else {
      myRequesy({
        url: 'follow/add',
        method: 'POST',
        data: {
          user_id: Detailsobj.user.id
        },
      }).then(res => {
        if (res.data.code === 0) {
          wx.showToast({
            title: '关注成功',
          })
          thst.getDynamicDetails()
        }

      })
    }

  },


  // 用户取消关注
  headleConfirm() {
    let that = this
    myRequesy({
      url: 'follow/take-off',
      method: 'POST',
      data: {
        user_id: that.data.Detailsobj.user.id
      }
    }).then(res => {
      if (res.data.code === 0) {
        wx.showToast({
          title: '已取消关注',
        })
        that.setData({
          follreveal: false,
        })
        that.getDynamicDetails()
      }
    })
  },


  //评论头部选项卡事件
  btnpre(e) {
    let {
      index
    } = e.currentTarget.dataset
    this.setData({
      index
    })
    let {
      select
    } = this.data
    select.forEach((item, i) => index === i ? item.status = true : item.status = false)
    this.setData({
      select,
      HeatPage: 1
    })
    let that = this
    if (index === 1) {
      this.setData({
        active: 1
      })
      that.getComment()
    } else {
      this.setData({
        active: 0
      })
      that.getComment()
    }

  },

  //  点击更多显示底部弹窗回调
  btnRome() {
    this.setData({
      Hireveal: true
    })
  },

  // 点击取消或遮罩层隐藏底部弹窗
  btnCancel() {
    this.setData({
      Hireveal: false,
      Hizfs: false,
      publish: false,
    })
  },

  // 获取写评论输入框的回调 ，弹窗 
  btnFocus() {
    this.setData({
      publish: true,
      Switch: true,
      values: ''
    })

  },

  //关闭用户删除回复/评论的回调 ，弹窗
  hiRemove() {
    this.setData({
      removeEnt: false,
      Hireveal: false,
      publish: false,
      Hizfs: false,
      publish: false
    })
  },

  // 用户动态转发事件
  btnForward(e) {
    this.setData({
      Hizfs: true,
    })
  },

  // 跳转到海报页
  headleTransmit() {
    let {
      Detailsobj
    } = this.data
    wx.navigateTo({
      url: '/pages/transmit/transmit?arr=' + JSON.stringify(Detailsobj.img_list) + '&nickName=' + Detailsobj.user.nickName + '&avatarUrl=' + Detailsobj.user.avatarUrl + '&qrcodeId=' + Detailsobj.id
    })
  },


  // 获取用户动态详情数据
  getDynamicDetails() {
    let that = this
    myRequesy({
      url: 'dynamic/detail',
      method: 'GET',
      data: {
        dynamic_id: that.data.detailsId
      },
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          Detailsobj: res.data.data
        })
        //其他状态
        let {
          likes_count,
          id,
          comments_count,
          is_likes
        } = res.data.data
        that.getTrigger(likes_count, id, comments_count, is_likes)
        // 关注
        let {
          listId
        } = that.data
        let obj = {
          id: res.data.data.user.id,
          follow_state: res.data.data.follow_state
        }
        if (listId.find(item => item.id == obj.id) !== undefined) {
          listId.splice(listId.findIndex(item1 => item1.id == obj.id), 1)
        }
        listId.push(obj)
        that.setData({
          listId
        })
        wx.setStorageSync('listId', listId)
      } else if (res.data.code === 1003) {
        // 关注
        let {
          deleteId
        } = that.data
        deleteId.push(parseInt(that.data.detailsId))
        wx.setStorageSync('deleteId', JSON.stringify(deleteId))
        let page = getCurrentPages()
        if (page.length > 1) {
          wx.navigateBack()
        }
        wx.switchTab({
          url: '/pages/index/index',
        })
      }

    })
  },

  //获取广告数据列表
  getadsList() {
    myRequesy({
      url: 'ads',
    }).then(res => {
      if (res.data.code === 0) {
        this.setData({
          absSipwerList: res.data.data
        })
      }
    })
  },

  // 获取用户动态/评论信息
  getComment() {
    let that = this
    myRequesy({
      url: 'dynamic/comments',
      data: {
        dynamic_id: that.data.detailsId,
        order_by: that.data.active === 0 ? 'createTime' : 'likes_count',
        page: that.data.HeatPage
      }
    }).then(res => {
      this.setData({
        commentlist: res.data.data,
        count: res.data.count,
        nextHeat: res.data.next_url
      })
    })
  },


  // 获取用户输入的内容
  headleAddInput(event) {
    this.setData({
      values: event.detail.value
    })

  },

  // 发表评论/回复信息回调
  headleSend() {
    let {
      values,
      Detailsobj,
      Switch,
      listIndex,
      commentlist,
      select,
      active
    } = this.data
    let that = this
    if (Switch) {
      wx.requestSubscribeMessage({
        tmplIds: ['0EScbOuy2FrgarTESiw7R2w9RfeA2SsFfz4qxqCJ4vg', '8pUorSTDgk0nqAU7v6GxRM3LP2RY_EmDgcrimSx1X0Q', 'Ke1p0Jn84Q_5XVYM6TT5aWb1Z4-oy7bJ2odnnH88kOE'],
        success(res) {
          myRequesy({
            url: 'comment/add',
            data: {
              dynamic_id: Detailsobj.id,
              content: values
            },
            method: 'POST',
          }).then(res => {
            if (res.data.code === 0) {
              that.setData({
                publish: false,
                HeatPage: 1,
              })
              that.getDynamicDetails()
              wx.showToast({
                title: '评论成功',
              })

              if (select[1].status) {
                select[1].status = false
                select[0].status = true
                that.setData({
                  select,
                  active: 0
                })
                that.getComment()
                return
              }

              that.getComment()
            } else {
              wx.showToast({
                title: '不能包含铭感词',
                icon: "none"
              })
            }
          })
        }
      })

    } else {
      myRequesy({
        url: 'reply-comment/add',
        method: 'POST',
        data: {
          comment_id: that.data.id,
          content: values
        }
      }).then(res => {
        console.log(res);
        if (res.data.code === 0) {
          that.setData({
            publish: false,
          })
          wx.showToast({
            title: '回复成功',
          })
          myRequesy({
            url: 'comment/detail',
            data: {
              comment_id: that.data.id
            }
          }).then(res => {
            if (res.data.code === 0) {
              commentlist[listIndex] = res.data.data
              that.setData({
                commentlist,
                publish: false
              })
              wx.showToast({
                title: '评论成功',
                icon: "none"
              })
            } else {
              wx.showToast({
                title: '不能包含铭感词',
                icon: "none"
              })
            }
          })
        }
      })
    }

  },





  //点击删除评论 获取对应的id
  headleShow(e) {
    console.log(e);
    this.setData({
      removeEnt: true,
      deleHiShow: false,
      deleteId: e.currentTarget.dataset.id,
      indexsta: e.currentTarget.dataset.indexsta
    })
  },


  //获取作者回复评论信息
  headleReply(e) {
    console.log(e);

    this.setData({
      publish: true,
      Switch: false,
      values: "",
      name: e.currentTarget.dataset.name,
      id: e.currentTarget.dataset.id,
      listIndex: e.currentTarget.dataset.listindex
    })
  },


  // 根据id删除对应的评论 /回复
  headleDelete() {
    let that = this
    let {
      deleHiShow,
      deleteId,
      commentlist,
      deleindex,
      dynamicid,
      indexsta,
      count
    } = this.data
    if (!deleHiShow) {
      myRequesy({
        url: 'comment/delete',
        method: 'POST',
        data: {
          comment_id: deleteId
        }
      }).then(res => {
        if (res.data.code === 0) {
          commentlist.splice(indexsta, 1)
          that.setData({
            count: count - 1,
            commentlist,
            removeEnt: false,
          })
          wx.showToast({
            title: '已删除',
          })
          that.getDynamicDetails()
        }

      })
    } else {
      myRequesy({
        url: 'reply-comment/delete',
        method: 'POST',
        data: {
          reply_id: deleteId
        }
      }).then(res => {
        if (res.data.code === 0) {
          myRequesy({
            url: 'comment/detail',
            data: {
              comment_id: dynamicid
            }
          }).then(res => {
            if (res.data.code === 0) {
              commentlist[deleindex] = res.data.data
              that.setData({
                commentlist,
                removeEnt: false,
              })
              wx.showToast({
                title: '已删除',
              })
            }
          })

        }
      })
    }

  },



  //点击删除回复 获取回复id
  btnRemove(e) {
    console.log(e);
    this.setData({
      removeEnt: true,
      deleHiShow: true,
      deleteId: e.currentTarget.dataset.id,
      dynamicid: e.currentTarget.dataset.dynamicid,
      deleindex: e.currentTarget.dataset.deleindex
    })
  },


  // 删除动态
  headleDeleteStatus() {
    let that = this
    myRequesy({
      url: 'dynamic/delete',
      method: 'POST',
      data: {
        dynamic_id: that.data.detailsId
      }
    }).then(res => {
      if (res.data.code === 0) {
        let {
          deleteId
        } = that.data
        deleteId.push(parseInt(that.data.detailsId))
        wx.setStorageSync('deleteId', JSON.stringify(deleteId))
        let page = getCurrentPages()
        if (page.length > 1) {
          setTimeout(() => {
            wx.navigateBack()
          }, 1000)
        } else {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }

      }


    })
  },


  //动态点赞
  headlePraise(e) {
    let {
      Detailsobj
    } = this.data
    if (Detailsobj.is_likes) {
      myRequesy({
        url: 'dynamic-likes/delete',
        method: 'POST',
        data: {
          dynamic_id: Detailsobj.id
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.getDynamicDetails()
        }

      })
    } else {
      myRequesy({
        url: 'dynamic-likes/add',
        method: 'POST',
        data: {
          dynamic_id: Detailsobj.id
        }
      }).then(res => {
        this.getDynamicDetails()
      })
    }

  },




  //获取当前点击动态的状态
  getTrigger(likes_count, id, comments, is_likes) {
    let that = this
    let {
      stateList
    } = that.data
    let obj = {
      likes_count,
      id,
      comments,
      is_likes
    }
    if (stateList.find(item => item.id == id) !== undefined) {
      stateList.splice(stateList.findIndex(item => item.id == id), 1)
    }
    stateList.push(obj)
    this.setData({
      stateList
    })
    wx.setStorageSync('stateList', JSON.stringify(stateList))
  },


  // 点击头像跳转到用户主页
  navHome(e) {
    console.log(e);
    wx.navigateTo({
      url: '/pages/dynamic/dynamic?id=' + e.currentTarget.dataset.id,
    })
  },


  // 点赞评论
  headleCoPraise(e) {
    let that = this
    let {
      commentlist
    } = that.data
    if (!e.currentTarget.dataset.status) {
      myRequesy({
        url: 'comment-likes/add',
        method: 'POST',
        data: {
          comment_id: e.currentTarget.dataset.id
        }
      }).then(res => {
        if (res.data.code === 0) {
          myRequesy({
            url: 'comment/detail',
            data: {
              comment_id: e.currentTarget.dataset.id
            }
          }).then(res => {
            if (res.data.code === 0) {
              commentlist[e.currentTarget.dataset.index] = res.data.data
              that.setData({
                commentlist
              })
            }
          })
        }
      })
    } else {
      myRequesy({
        url: 'comment-likes/delete',
        method: 'POST',
        data: {
          comment_id: e.currentTarget.dataset.id
        }
      }).then(res => {
        if (res.data.code === 0) {
          myRequesy({
            url: 'comment/detail',
            data: {
              comment_id: e.currentTarget.dataset.id
            }
          }).then(res => {
            if (res.data.code === 0) {
              commentlist[e.currentTarget.dataset.index] = res.data.data
              that.setData({
                commentlist
              })
            }
          })
        }
      })
    }
  },


  // 预览动态图片
  btnPreview(e) {
    let current = e.currentTarget.dataset.item1
    let urls = e.currentTarget.dataset.item
    wx.previewImage({
      current,
      urls
    })
  },

  // 跳转到标签页
  btnNavTag(e) {
    if (e.currentTarget.dataset.status) {
      wx.navigateTo({
        url: '/pages/hddetails/hddetails?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/tag/tag?id=' + e.currentTarget.dataset.id,
      })
    }

  },

  // 上拉获取评论下一页数据
  getNextCommentPage() {
    let that = this
    if (that.data.nextHeat !== null) {
      that.data.HeatPage += 1
      myRequesy({
        url: 'dynamic/comments',
        data: {
          dynamic_id: that.data.detailsId,
          order_by: that.data.active === 0 ? 'createTime' : 'likes_count',
          page: that.data.HeatPage
        }
      }).then(res => {
        this.setData({
          commentlist: [...that.data.commentlist, ...res.data.data],
          count: res.data.count,
          nextHeat: res.data.next_url
        })
      })
      return
    }
  },

  // 跳转到新人用户页
  headleTask() {
    wx.navigateTo({
      url: '/pages/mission/mission',
    })
  },



  //跳转到对应的小程序或web-view
  headleNavProgram(e) {
    let {
      appid,
      state,
      path,
      url
    } = e.currentTarget.dataset.item
    if (state == 0) {
      wx.navigateToMiniProgram({
        appId: appid,
        path,
        success(res) {},
      })
      return
    }
    let data = encodeURIComponent(url)
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + data,
    })

  },

  headleReport() {
    wx.navigateTo({
      url: '/pages/feedback/feedback?vid=' + this.data.Detailsobj.id,
    })
  },

  // 第一张轮播图跳转到大赛页
  headleNavMega() {
    wx.navigateTo({
      url: '/pages/megagame/megagame',
    })
  },
  // 第二张轮播图跳转到新人任务页
  headleNavMiss() {
    wx.navigateTo({
      url: '/pages/mission/mission',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDynamicDetails()
    // 新人专享
    if (wx.getStorageSync('conceal')) {
      this.setData({
        shut: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getNextCommentPage()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {

    return {
      title: this.data.Detailsobj.is_own ? "我发布了一组作品，快来帮我点个赞" : "这组摄影作品很精彩，快来一起围观",
      path: "/pages/details/details?id=" + this.data.Detailsobj.id,
      imageUrl: this.data.Detailsobj.img_list[0]
    }
  }
})